package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo08 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        try (Connection conn = DBUtils.getConn()) {
            String sql = "select password from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            // 替换? 把用户名替换到SQL语句中
            ps.setString(1,username);
            // 执行查询
            ResultSet rs = ps.executeQuery();
            // 判断是否查询到数据
            if (rs.next()){
                // 判断用户输入的密码和数据库中查询到的密码是否一致
                if (password.equals(rs.getString(1))){
                    System.out.println("登陆成功!");
                }else {
                    System.out.println("密码错误!");
                }
            }else {
                System.out.println("用户名不存在");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
